<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class FgcmsProductBean {
	
	public function __construct() {
	// contructor function
	}
	
	public static function count_product_by_name($db,$key) {
		
		
    	$strSQL = sprintf("SELECT p.*".
							" FROM ".TBL_PRODUCT." p ".
							" WHERE p.product_name LIKE '%%$key%%' ");
							
    	return count($db->get_results($strSQL));
    }
	
	public static function get_product_by_name_paging($db,$paging,$path,$key) {
		$page = isset ( $_GET[$path]) ? intval ( $_GET[$path] ) : 1;
		$rows_per_page= $paging;
		$page_start = ( $page - 1 ) * $rows_per_page;
		
    	$strSQL = sprintf("SELECT p.*,t.*,l.*,s.specials_new_products_price,s.expires_date,f.featured_new_products_price,f.expires_date as featured_expires_date, u.symbol_left ".
							" FROM ".TBL_PRODUCT." p ".
							" LEFT JOIN ".TBL_PRODUCT_GROUP2CAT." t ON p.product_group_id = t.product_group_id ".
							" LEFT JOIN ".TBL_PRODUCT_GROUP." l ON l.product_group_id = t.product_group_id ".
							" LEFT JOIN " . TBL_SPECIAL . " s ON s.product_id = p.product_id".
							" LEFT JOIN " . TBL_FEATURED . " f ON f.product_id = p.product_id".
							" LEFT JOIN " . TBL_CURRENCY . " u ON u.currency_id = p.currency_id".
							" WHERE p.product_name LIKE '%%$key%%' LIMIT $page_start,$rows_per_page");
    	return $db->get_results($strSQL);
    }
	
	/*returns the whole active left sided country table
	 */ 
	public static function get_product_by_cat($db,$catid) {
		if($catid!="")
			$cond = " WHERE t.cat_id = '$catid'";
    	$strSQL = sprintf("SELECT p.*,t.*,l.* FROM ".TBL_PRODUCT_GROUP2CAT." t ".
							" LEFT JOIN ".TBL_PRODUCT." p ON p.product_id = t.product_id ".
							" LEFT JOIN ".TBL_PRODUCT_GROUP." l ON l.product_group_id = t.product_group_id ".
							" $cond");
    	return $db->get_results($strSQL);
    }
	 
	public static function count_product_by_cat($db,$catid) {
		if($catid!="")
			$cond = " WHERE t.cat_id = '$catid'";
    	$strSQL = sprintf("SELECT p.*,t.*,l.* FROM ".TBL_PRODUCT_GROUP2CAT." t ".
							" LEFT JOIN ".TBL_PRODUCT." p ON p.product_id = t.product_id ".
							" LEFT JOIN ".TBL_PRODUCT_GROUP." l ON l.product_group_id = t.product_group_id ".
							" $cond");
    	return count($db->get_results($strSQL));
    }
	public static function get_product_by_cat_paging($db,$paging,$path,$catid) {
		$page = isset ( $_GET[$path]) ? intval ( $_GET[$path] ) : 1;
		$rows_per_page= $paging;
		$page_start = ( $page - 1 ) * $rows_per_page;
		
    	$strSQL = sprintf("SELECT p.*,t.*,l.*,s.specials_new_products_price,s.expires_date,f.featured_new_products_price,f.expires_date as featured_expires_date, u.symbol_left ".
							" FROM ".TBL_PRODUCT_GROUP2CAT." t ".
							" LEFT JOIN ".TBL_PRODUCT." p ON p.product_id = t.product_id ".
							" LEFT JOIN ".TBL_PRODUCT_GROUP." l ON l.product_group_id = t.product_group_id ".
							" LEFT JOIN " . TBL_SPECIAL . " s ON s.product_id = p.product_id".
							" LEFT JOIN " . TBL_FEATURED . " f ON f.product_id = p.product_id".
							" LEFT JOIN " . TBL_CURRENCY . " u ON u.currency_id = p.currency_id".
							" WHERE t.cat_id = '$catid' LIMIT $page_start,$rows_per_page");
		$res = 	$db->get_results($strSQL);
    	return $res;
    }
	
	public static function getProductbyId($db, $id) {
    	$strSQL = sprintf("SELECT p.*, c.cat_name,s.specials_new_products_price,s.expires_date,f.featured_new_products_price,f.expires_date as featured_expires_date, u.symbol_left FROM " . TBL_PRODUCT . " p ".
				" LEFT JOIN " . TBL_CURRENCY . " u ON u.currency_id = p.currency_id".
				" LEFT JOIN " . TBL_SPECIAL . " s ON s.product_id = p.product_id".
				" LEFT JOIN " . TBL_FEATURED . " f ON f.product_id = p.product_id".
				" LEFT JOIN " . TBL_CATEGORY . " c ON p.product_group_id = c.cat_ID WHERE p.product_id='$id'");
    			
    	$res = $db->get_row($strSQL);
    	
    	return $res;
    }
	
	public static function countallSpecial($db) {
    	$strSQL = sprintf("SELECT p.product_id, p.product_name, p.product_price, p.product_thumbnail, s.specials_new_products_price, s.status, s.special_id, c.symbol_left".
		 				" FROM ". TBL_SPECIAL ." s ". 
						"LEFT JOIN ". TBL_PRODUCT . " p ON s.product_id = p.product_id ".
						"LEFT JOIN " . TBL_CURRENCY . " c ON s.currency_id = c.currency_id ");
		
		$res = $db->get_results($strSQL);
		return count($res);
    }
	
	public static function getSpecialLimit($db) {
    	$strSQL = sprintf("SELECT p.product_id, p.product_name, p.product_price, p.product_thumbnail, s.specials_new_products_price, s.status, s.special_id, c.symbol_left".
						" FROM ". TBL_SPECIAL ." s ". 
						"LEFT JOIN ". TBL_PRODUCT . " p ON s.product_id = p.product_id ".
						"LEFT JOIN " . TBL_CURRENCY . " c ON s.currency_id = c.currency_id ".
						" ORDER BY s.position  ASC LIMIT 0,2");
		
		$res = $db->get_results($strSQL);
		return $res;
    }
	
	public static function getAllSpecial_paging($db,$paging,$path) {
		$page = isset ( $_GET[$path]) ? intval ( $_GET[$path] ) : 1;
		$rows_per_page= $paging;
		$page_start = ( $page - 1 ) * $rows_per_page;
    	$strSQL = sprintf("SELECT p.product_id, p.product_name, p.product_price, p.product_thumbnail, s.specials_new_products_price, s.status, s.special_id, c.symbol_left".
		 				" FROM ". TBL_SPECIAL ." s ". 
						"LEFT JOIN ". TBL_PRODUCT . " p ON s.product_id = p.product_id ".
						"LEFT JOIN " . TBL_CURRENCY . " c ON s.currency_id = c.currency_id ".
						" ORDER BY s.special_id  LIMIT $page_start,$rows_per_page");
		
		$res = $db->get_results($strSQL);
		return $res;
    }
	
	public static function countallFeatured($db) {
    	$strSQL = sprintf("SELECT p.product_id, p.product_name, p.product_price, p.product_thumbnail, f.featured_new_products_price,f.status, f.featured_id, c.symbol_left ".
						"FROM ". TBL_FEATURED ." f ". 
						"LEFT JOIN ". TBL_PRODUCT . " p ON f.product_id = p.product_id ".
						"LEFT JOIN " . TBL_CURRENCY . " c ON f.currency_id = c.currency_id ");
		
		$res = $db->get_results($strSQL);
		return count($res);
    }
	
	public static function getFeaturedLimit($db) {
    	$strSQL = sprintf("SELECT p.product_id, p.product_name, p.product_price, p.product_thumbnail, f.featured_new_products_price,f.status, f.featured_id, c.symbol_left ".
		 				"FROM ". TBL_FEATURED ." f ". 
						"LEFT JOIN ". TBL_PRODUCT . " p ON f.product_id = p.product_id ".
						"LEFT JOIN " . TBL_CURRENCY . " c ON f.currency_id = c.currency_id ".
						" ORDER BY f.position  ASC LIMIT 0,2");
		
		$res = $db->get_results($strSQL);
		return $res;
    }
	
	public static function getAllFeatured_paging($db,$paging,$path) {
		$page = isset ( $_GET[$path]) ? intval ( $_GET[$path] ) : 1;
		$rows_per_page= $paging;
		$page_start = ( $page - 1 ) * $rows_per_page;
    	$strSQL = sprintf("SELECT p.product_id, p.product_name, p.product_price, p.product_thumbnail, f.featured_new_products_price,f.status, f.featured_id, c.symbol_left ".
		 				"FROM ". TBL_FEATURED ." f ". 
						"LEFT JOIN ". TBL_PRODUCT . " p ON f.product_id = p.product_id ".
						"LEFT JOIN " . TBL_CURRENCY . " c ON f.currency_id = c.currency_id ".
						"ORDER BY f.featured_id LIMIT $page_start,$rows_per_page");
		
		$res = $db->get_results($strSQL);
		return $res;
    }
	
	/**
     * Get a photos with inputted product id.
     * @param $id id of the product.
     * @return a product object with inputted id.
     * Notes: product object contain fields is same as defined in database.  
     */
    function PhotosByProductId($db, $id) {
    	$sql = "SELECT * FROM " . TBL_PRODUCT_PHOTOS . " p ".
				" WHERE p.product_id='$id'";
    			
    	$res = $db->get_results($sql);
    	return $res;
    }
	
}
?>
